package main

import (
	"gofw-im/lib/define"
	"gofw-im/lib/logger"

	"github.com/go-redis/redis"
)

var (
	RedisCli *redis.Client
)

func InitRedis() (err error) {
	RedisCli = redis.NewClient(&redis.Options{
		Addr:     conf.Base.RedisAddr,
		Password: conf.Base.RedisPw,        // no password set
		DB:       conf.Base.RedisDefaultDB, // use default DB
	})
	if pong, err := RedisCli.Ping().Result(); err != nil {
		logger.Error.Print("RedisCli Ping Result pong: %s,  err: %s", pong, err)
	}
	logger.Info.Printf("Init redis to listen: %s", conf.Base.RedisAddr)
	go func() {
		redisSub := RedisCli.Subscribe(define.REDIS_SUB)
		ch := redisSub.Channel()
		for {
			msg, ok := <-ch
			if !ok {
				logger.Warning.Printf("redisSub Channel !ok: %v", ok)
				break
			}
			push(msg.Payload)
			if conf.Base.IsDebug == true {
				logger.Info.Printf("redisSub Subscribe msg : %s", msg.Payload)
			}
		}
	}()
	return
}
